package DataProcessing;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.bg.PixelBoundaryBackground;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.font.scale.*;
import com.kennycason.kumo.image.AngleGenerator;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
import com.kennycason.kumo.nlp.tokenizers.ChineseWordTokenizer;
import com.kennycason.kumo.palette.ColorPalette;

public class Visualizer {
    private List<String> comments;
    private String outputImageName = "src\\main\\resources\\out.png";
    private String backgroundPath = "src\\main\\resources\\back.webp";
    private FontScalar scalar = new LinearFontScalar(20, 60);
    private Font font = new Font("宋体", 1, 50);
    private AngleGenerator angle = new AngleGenerator(0);
    private int density = 1;
    private Dimension dim = new Dimension(1000, 1000);
    private ColorPalette colors = new ColorPalette(new Color(0x4055F1), new Color(0x408DF1), new Color(0x40AAF1),
            new Color(0x40C5F1), new Color(0x40D3F1), new Color(0xFFFFFF));

    public String getOutputImageName() {
        return outputImageName;
    }

    public ColorPalette getColors() {
        return colors;
    }

    public void setColors(ColorPalette colors) {
        this.colors = colors;
    }

    public void setOutputImageName(String outputImageName) {
        this.outputImageName = outputImageName;
    }

    public String getBackgroundPath() {
        return backgroundPath;
    }

    public void setBackgroundPath(String backgroundPath) throws IOException {
        this.backgroundPath = backgroundPath;
        var img = ImageIO.read(new File(backgroundPath));
        dim = new Dimension(img.getWidth(), img.getHeight());
    }

    public FontScalar getScalar() {
        return scalar;
    }

    public void setScalar(FontScalar scalar) {
        this.scalar = scalar;
    }

    public Font getFont() {
        return font;
    }

    public void setFont(Font font) {
        this.font = font;
    }

    public AngleGenerator getAngle() {
        return angle;
    }

    public void setAngle(AngleGenerator angle) {
        this.angle = angle;
    }

    public int getDensity() {
        return density;
    }

    public void setDensity(int density) {
        this.density = density;
    }

    public Dimension getDim() {
        return dim;
    }

    public void setDim(Dimension dim) {
        this.dim = dim;
    }

    public Visualizer(List<String> comm) {
        comments = comm;
    }

    public void getWordCloud(List<String> words) {
        FrequencyAnalyzer fqa = new FrequencyAnalyzer();
        fqa.setWordFrequenciesToReturn(words.size());
        fqa.setWordTokenizer(new ChineseWordTokenizer());
        var frqAna = fqa.load(words);

        System.out.println(dim.toString());
        var wordCloud = new WordCloud(dim, CollisionMode.PIXEL_PERFECT);
        wordCloud.setColorPalette(colors);
        wordCloud.setBackgroundColor(Color.white);
        try{
            wordCloud.setBackground(new PixelBoundaryBackground(backgroundPath));
        } catch (IOException e) {
            System.err.println("[VS] 背景图无效");
        }
        
        wordCloud.setPadding(density);
        wordCloud.setFontScalar(scalar);// 字体放缩
        wordCloud.setKumoFont(new KumoFont(font));// 字体样式
        // wordCloud.setBackground(new CircleBackground(250));// 蒙版
        wordCloud.setAngleGenerator(angle);
        wordCloud.build(frqAna);
        wordCloud.writeToFile(outputImageName);
    }
    
    public void getWordCloud() {
        getWordCloud(comments);
    }

    public static void main(String[] args) {
        System.out.println(args.length);
    }
}
